package com.rocketmq.base.example.guide.simple.sync;

import com.rocketmq.base.example.Const;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Auther: tianchao
 * @Date: 2021/9/3 16:15
 * @Description:
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new
                DefaultMQProducer("SyncProducer-group-name");
        producer.setNamesrvAddr(Const.nameServer);
        producer.start();
        for (int i = 0; i < 2; i++) {

            Thread.sleep(1000);
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("TopicTest1" /* Topic */,
                    "TagA"+1 /* Tag */,
                    "key"+i,
                    ("Hello RocketMQ " +
                            i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }
    /**
     * ConsumeMessageThread_5 Receive New Messages: MessageExt [queueId=3, storeSize=190, queueOffset=13, sysFlag=0, bornTimestamp=1630904262509, bornHost=/192.168.1.120:64289, storeTimestamp=1630904273351, storeHost=/192.168.1.221:10911, msgId=C0A801DD00002A9F000000000088C4E6, commitLogOffset=8963302, bodyCRC=613185359, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest1', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=14, KEYS=key0, CONSUME_START_TIME=1630904262533, UNIQ_KEY=C0A80178327418B4AAC21C87CF6C0000, WAIT=true, TAGS=TagA1}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 48], transactionId='null'}]
     * 接受消息======================================
     */
}
